﻿using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings.RouteSetting
{
    public partial class RouteChannel : UserControl
    {
        public delegate void RouteChannelSelectedChangedEventHandler(RouteChannel rc);
        public event RouteChannelSelectedChangedEventHandler SelectChanged;

        public delegate void RouteChannelEndDragEventHandler(RouteChannel rc);
        public event RouteChannelEndDragEventHandler EndDrag;

        private const int ctl_height = 60;
        private const int lbl_line_height = 20;

        private bool selected = false;

        public RouteMap Map { get; set; }

        public bool Selected
        {
            get { return selected; }
            set
            {
                selected = value;

                lblName.ForeColor = selected ? Color.Red : Color.Blue;

                if (!Map.map_loading && SelectChanged != null)
                {
                    SelectChanged(this);
                }
            }
        }

        private bool highlight = false;
        public bool Highlight
        {
            get { return highlight; }
            set
            {
                highlight = value;

                this.BackColor = highlight ? Color.LightSkyBlue : Color.Transparent;

                if (highlight)
                {
                    this.BringToFront();
                }
            }
        }

        public string CameraId { get; set; }

        private string cameraName = "摄像机";
        public string CameraName
        {
            get { return cameraName; }
            set
            {
                cameraName = value;
                SetCameraName(cameraName);
            }
        }

        public string ServerId { get; set; }

        public dl_map_channel Channel { get; set; }

        public RouteChannel()
        {
            InitializeComponent();

            Init();
        }

        private void SetCameraName(string name)
        {
            int line = 1;

            if (!string.IsNullOrEmpty(name))
            {
                line = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(name.Length) / 4));
            }

            this.Height = ctl_height + (line - 1) * lbl_line_height;
            this.lblName.Height = line * lbl_line_height;

            lblName.Text = name;

            toolTip1.SetToolTip(picIcon, name);
        }

        private void Init()
        {
            this.BackColor = Color.Transparent;
            this.lblName.ForeColor = Color.Blue;

            this.MouseEnter += RouteChannel_MouseEnter;
            this.MouseDown += RouteChannel_MouseDown;
            this.MouseUp += RouteChannel_MouseUp;
            this.MouseMove += RouteChannel_MouseMove;
            this.Click += RouteChannel_Click;

            this.picIcon.MouseEnter += RouteChannel_MouseEnter;
            this.picIcon.MouseDown += RouteChannel_MouseDown;
            this.picIcon.MouseUp += RouteChannel_MouseUp;
            this.picIcon.MouseMove += RouteChannel_MouseMove;
            this.picIcon.Click += RouteChannel_Click;
            this.picIcon.MouseLeave += PicIcon_MouseLeave;

            this.lblName.MouseEnter += RouteChannel_MouseEnter;
            this.lblName.MouseDown += RouteChannel_MouseDown;
            this.lblName.MouseUp += RouteChannel_MouseUp;
            this.lblName.MouseMove += RouteChannel_MouseMove;
            this.lblName.Click += RouteChannel_Click;



        }

        private void PicIcon_MouseLeave(object sender, EventArgs e)
        {
            //toolTip1.Hide(picIcon.FindForm());
        }

        private void RouteChannel_Click(object sender, EventArgs e)
        {
            if (!Map.DesignContext.EditMode)
            {
                this.Selected = !this.Selected;
            }

            this.BringToFront();
        }

        private void RouteChannel_MouseEnter(object sender, EventArgs e)
        {
            (sender as Control).Cursor = Cursors.Hand;


            //toolTip1.Show(this.cameraName, this.FindForm());
        }

        private void RouteChannel_MouseDown(object sender, MouseEventArgs e)
        {
            if (Map.DesignContext.EditMode)
            {
                Map.DesignContext.CurrCamera = this;

                Map.DesignContext.LastX = e.X;
                Map.DesignContext.LastY = e.Y;
                Map.DesignContext.DragCamera = true;
            }
        }

        private void RouteChannel_MouseUp(object sender, MouseEventArgs e)
        {
            if (Map.DesignContext.EditMode)
            {
                if (Map.DesignContext.DragCamera)
                {
                    Map.DesignContext.DragCamera = false;
                    Map.DesignContext.CurrCamera.Cursor = Cursors.Hand;

                    if (EndDrag != null)
                    {
                        EndDrag(this);
                    }
                }
                else
                {
                    if (this.ContextMenuStrip == null)
                    {
                        this.ContextMenuStrip = cmCamera;
                    }
                }
            }
        }

        private void RouteChannel_MouseMove(object sender, MouseEventArgs e)
        {
            if (Map.DesignContext.EditMode && Map.DesignContext.DragCamera && Map.DesignContext.CurrCamera != null)
            {
                Map.DesignContext.CurrCamera.Cursor = Cursors.SizeAll;
                Map.DesignContext.CurrCamera.Left += (e.X - Map.DesignContext.LastX);
                Map.DesignContext.CurrCamera.Top += (e.Y - Map.DesignContext.LastY);
            }
        }

        public void Rename()
        {

        }
    }
}
